/**
 * Created by luckyjing on 16/2/8.
 */
var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    User = require('mongoose').model('User');
module.exports = function () {
    // done 为鉴权完成时需要调用的回调函数
    passport.use(new LocalStrategy(function (username, password, done) {
        User.findOne({
            username: username
        }, function (err, user) {
            if (err) {
                return done(err);
            }
            if (!user) {
                return done(null, false, {
                    message: '未知用户'
                });
            }
            if (!user.authenticate(password)) {
                return done(null, false, {
                    message: '错误的密码'
                })
            }
            // 认证成功后将会把user传递给下一个执行对象
            return done(null, user);
        })
    }))
};